package com.hadwinling.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hadwinling.blog.entity.Admin;
import com.hadwinling.blog.entity.Message;
import com.hadwinling.blog.entity.Notice;
import com.hadwinling.blog.service.MessageService;
import com.hadwinling.blog.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 公告
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/8/20 14:28
 */
@Controller
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private MessageService messageService;
    @Value("${admin.article.page.pageSize}")
    private int pageSize;
    @Value("${page.navigatePage}")
    private int navigatePage;


    /**
     * 跳转到管理中心的公告页面
     * @param pageNum
     * @param model
     * @return
     */
    @GetMapping("/admin/notice")
    public String toNoticePage(@RequestParam(name = "pageNum" ,defaultValue = "1") int pageNum, Model model){
        PageHelper.startPage(pageNum,pageSize);
        List<Notice> noticeList = noticeService.selectAllByStateOrderByCreatetimeDesc(1);
        PageInfo<Notice> pageInfo = new PageInfo<>(noticeList,navigatePage);
        //当前页码
        int currPage = pageInfo.getPageNum();
        //总记录
        Long total = pageInfo.getTotal();
        //总页数
        int totalPage = pageInfo.getPages();
        //所有页面
        int [] pages = pageInfo.getNavigatepageNums();
        //保存分页信息
        model.addAttribute("currPage",currPage);
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pages",pages);
        model.addAttribute("noticeList",noticeList);
        return "AdminCenter/notice";
    }

    /**
     * 跳转到发布公告页面
     * @return
     */
    @GetMapping("/admin/addNotice")
    public String toaddNoticePage(HttpServletRequest request,Model model){
        Admin admin = (Admin) request.getSession().getAttribute("currAdmin");
        model.addAttribute("admin",admin);
        return "AdminCenter/noticeAdd";
    }


    /**
     * 执行发布公告操作
     * @param request
     * @param notice
     * @return
     */
    @PostMapping("/admin/addNotice")
    public String addNotice(HttpServletRequest request,Notice notice){
        Admin admin = (Admin) request.getSession().getAttribute("currAdmin");
        int adminId = admin.getId();
        notice.setAdminid(adminId);
        notice.setState(1);
        if (admin.getPower()==1){
            //若是admin超级管理者的权限为1。则将权限设置值为1
            notice.setPower(1);
        }else{
            notice.setPower(0);
        }
        notice.setCreatetime(new Date());
        notice.setTop(0);
//        System.out.println("notiec:"+notice);
        noticeService.insertSelective(notice);

      /*  Message message = new Message();
        message.setState(1);
        message.setIsread(0);
        message.setNoticeid(notice.getId());
        message.setCreatetime(new Date());
        messageService.insertSelective(message);*/

        return "redirect:/admin/notice";
    }

    /**
     * 删除公告
     * @param noticeid
     * @param request
     * @return
     */
    @PostMapping("/admin/deleteNotie")
    @ResponseBody
    public String deleteNotie(@RequestParam(name = "noticeid") int noticeid,HttpServletRequest request){
        Notice notice = noticeService.selectByPrimaryKey(noticeid);
        Admin admin = (Admin) request.getSession().getAttribute("currAdmin");
        int adminId = admin.getPower();
        //判断权限
        if (adminId==1){
            //这里是超级管理者
            //拥有最大的权限
            notice.setState(0);
        }else{
            //这里是普通的管理者
            //匹配是否是自己发的公告并执行删除操作。若不是自己的公告则没有权限进行删除
            if (adminId == notice.getAdminid()){
                //匹配到自己发布的公告，进行删除操作
                notice.setState(0);
                return "1";
            }else {
                //没有匹配到自己发布的公告，提示：没有权限
                return "0";
            }
        }
        return "1";
    }

    /**
     * 执行置顶操作
     * @param noticeid
     * @param powerstate
     * @param request
     * @return
     */
    @PostMapping("/admin/addNoticeTop")
    @ResponseBody
    public String addNoticeTop(@RequestParam(name = "noticeid") int noticeid,
                               @RequestParam(name = "powerstate") int powerstate,
                               HttpServletRequest request){
        List<Notice> noticeTopList = noticeService.selectAllByTopAndStateAndPowerstate(1, 1, powerstate);
        if (noticeTopList.size()==1){
            //不能置顶，已经有置顶的了
            return "0";
        }
        Notice notice = noticeService.selectByPrimaryKey(noticeid);

        Admin admin = (Admin) request.getSession().getAttribute("currAdmin");
        int power = admin.getPower();
        int adminId = admin.getId();

        //判断权限
        if (power==1){
            //这里是超级管理者
            //拥有最大的权限
            noticeService.updateTopById(1,noticeid);
        }else{
            //这里是普通管理者
            //这里需要去匹配对应的公告，如是普通用户管理者自己发布的公告，则进行置顶操作
            //否则，没有权限
            if (adminId == notice.getAdminid()){
                //匹配成功
                noticeService.updateTopById(1,noticeid);
                return "1";
            }else{
                //匹配失败
                return "0";
            }
        }
        return "1";
    }




    /**
     * 执行取消置顶操作
     * @param noticeid
     * @param request
     * @return
     */
    @PostMapping("/admin/noNoticeTop")
    @ResponseBody
    public String noNoticeTop(@RequestParam(name = "noticeid") int noticeid,
                              @RequestParam(name = "powerstate") int powerstate,
                              HttpServletRequest request){
        List<Notice> noticeTopList = noticeService.selectAllByTopAndStateAndPowerstate(1, 1, powerstate);
        if (noticeTopList.size()!=1){
            //不能执行取消置顶操作
            return "0";
        }
        Notice notice = noticeService.selectByPrimaryKey(noticeid);

        Admin admin = (Admin) request.getSession().getAttribute("currAdmin");
        int power = admin.getPower();
        int adminId = admin.getId();

        //判断权限
        if (power==1){
            //这里是超级管理者
            //拥有最大的权限
            noticeService.updateTopById(0,noticeid);
        }else{
            //这里是普通管理者
            //这里需要去匹配对应的公告，如是普通用户管理者自己发布的公告，则进行置顶操作
            //否则，没有权限
            if (adminId == notice.getAdminid()){
                //匹配成功
                noticeService.updateTopById(0,noticeid);
                return "1";
            }else{
                //匹配失败
                return "0";
            }
        }
        return "1";
    }


}
